package com.suxin.websocket.handler;

import com.suxin.websocket.model.BaseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @classname DefaultHandler
 * @description [ 默认处理 ]
 * @date 2022/3/24 14:50
 */
@Component
@Slf4j
public class DefaultHandler extends AbstractHandler<BaseModel> implements ApplicationContextAware {

    private List<Handler> handlers;

    public DefaultHandler() {
    }

    @Override
    public void handler0(BaseModel data) {
        if (CollectionUtils.isEmpty(handlers)) {
            return;
        }
        handlers.stream()
                .filter(item -> item.isSupport(data))
                .forEach(handler -> handler.handler(data));
    }

    @Override
    public boolean isSupport(BaseModel date) {
        return false;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        handlers = applicationContext.getBeanProvider(Handler.class)
                .stream().collect(Collectors.toList());
    }
}